<?php
/**
*
* ClassName: Unlock_sina_library
*
* 解锁新浪html页面搜索封IP
*
* @author hejigui <hejigui@soften.cn>
*
*/
class Unlock_sina_library {

	/**
	* send_http
	*
	* @access public
	* @param array $parameters xxxxxx [Must]
	* @return string $mixed
	*/
	public function send_http($parameters) {
		$url_info = parse_url($parameters['url']);

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $parameters['url']);

		$refer = 'http://s.weibo.com/weibo/dfff&Refer=STopic_box';
		curl_setopt ( $ch, CURLOPT_REFERER, $refer );

		curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20100101 Firefox/22.0');
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($ch, CURLOPT_TIMEOUT, 5);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,2);


        if($parameters['proxy']) {
        	curl_setopt($ch, CURLOPT_PROXY, $parameters['proxy']);
            curl_setopt($ch, CURLOPT_PROXYTYPE , CURLPROXY_SOCKS5);
        }
        
        //增加针对cookie的解封
        if($parameters['cookie_str'] == "ok") {
        	curl_setopt($ch, CURLOPT_COOKIE, $parameters['cookie']);
        }

		if($parameters['upload_files']){
			//var_dump($upload_files);
			foreach($parameters['upload_files'] as $key => $file) {
				$parameters['post_data'][$key] = '@' . realpath($file);
			}

			//var_dump($parameters['post_data']);exit;
			curl_setopt($ch, CURLOPT_POST, true);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters['post_data']);
		}elseif($parameters['post_data']){
			curl_setopt($ch, CURLOPT_POST, true);
			curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($parameters['post_data']));
		}

		if($parameters['cookie_file_name']){
			$cookie_file = $parameters['cookie_file_name'];
			curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
			curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
		}


		$rs = curl_exec($ch);
		$http_info = curl_getinfo($ch);

		if($http_info['http_code'] != 200 && $http_info['http_code'] != 302 ){
			$http_info['error_code'] = curl_errno($ch);
			$http_info['error_info'] = curl_error($ch);

			curl_close($ch);
			return $http_info;
		}

		curl_close($ch);

		return $rs;
	}

	/**
	* 获取验证码图片的值
	*
	* @access public
	* @param string $file_name 验证码在本地的图片地址路径 [Must]
	* @param string $yzm_platform 破解验证码平台 [Must]
	* @return string $mixed
	*/
	public function get_yzm($file_name,$yzm_platform) {
		if(empty($file_name) || empty($yzm_platform)){
			$this->_error = 'get_yzm param is error !!';
			return false;
		}

		$method_name = "get_yzm_by_{$yzm_platform}";
		return $this->$method_name($file_name);


		//return $this->get_yzm_by_uuppi($file_name);
		//return $this->get_yzm_by_dama2($file_name);
		//return $this->get_yzm_by_qqsuperman($file_name);
	}

	/**
	* get_yzm_by_dama2
	*
	* @access public
	* @param string $file_name 验证码在本地的图片地址路径 [Must]
	* @return string $mixed
	*/
	public function get_yzm_by_dama2($file_name) {
		//验证码类型ID	http://wiki.dama2.com/index.php?n=ApiDoc.GetSoftIDandKEY
		//错误码定义表	http://wiki.dama2.com/index.php?n=ApiDoc.ErrDef

		$userName = 'test';
		$passWord = 'test';
		$codeType = 42;

		require 'yzm/dama2Lib/Dama2CurlApi.php';

		$dama2Api = new Dama2Api($userName, $passWord);
		$user_info = $dama2Api->read_info();
		//var_dump($user_info);

		//获取用户信息
		if($user_info['ret'] < 0){
			$this->_error = "get_yzm_by_dama2 read_info is error !! user_info:".json_encode($user_info);
			return false;
		}

		//获取验证码信息
		$get_yzm_info = $dama2Api->decode($file_name, $codeType);
		//var_dump($get_yzm_info);

		if($get_yzm_info['ret'] < 0){
			$this->_error = "get_yzm_by_dama2 decode is error !! get_yzm_info:".json_encode($get_yzm_info);
			return false;
		}

		//获取验证码值
		$rs = $dama2Api->get_result($get_yzm_info['id']);
		//var_dump($rs);

		if($rs['ret'] < 0){
			$this->_error = "get_yzm_by_dama2 get_result is error !! rs:".json_encode($rs);
			return false;
		}

		return $rs['result'];
	}

	/**
	* 通过uuwise平台 获取验证码
	*
	* @access public
	* @param string $file_name 验证码在本地的图片地址路径 [Must]
	* @return string $mixed
	*/
	public function get_yzm_by_uuapi($file_name) {
		//错误代码大全 http://www.uuwise.com/allErrorCode.html
		//codetype	http://www.uuwise.com/price.html
		//技术电话: 18537168728


		$softID = '93723';
		$softKey = 'e460983e06714eb7a3ed789dd74fd5da';
		$userName = 'hexuan1922';
		$passWord = 'SofteN@2015';
		$codeType = 1004;

		require_once 'yzm/uuwise/uuapi.php';
		$obj = new uuApi;
		$obj->setSoftInfo($softID, $softKey);//初始化UU平台
		$loginStatus = $obj->userLogin($userName, $passWord);//登录

		if ($loginStatus < 1 ){
			$obj->clean_login_file();
			$this->_error = "get_yzm uuApi userLogin is error !! loginStatus:{$loginStatus}";
			return false;
		}

		//计算验证码
		$rs = $obj->autoRecognition($file_name, $codeType);

		if($rs < 0 || strlen($rs)>10){
			$this->_error = "get_yzm uuApi autoRecognition is error !! error_code:{$rs}";
			return false;
		}

		return $rs;
	}


	/**
	* 通过qq超人平台 获取验证码
	*
	* @access public
	* @param string $file_name 验证码在本地的图片地址路径 [Must]
	* @return string $mixed
	*/
	public function get_yzm_by_qqsuperman($file_name) {
		//错误码  http://www.sz789.net/showAPI.aspx?id=6

		//QQ超人打码账号配置信息
		$user = 'hexuan1922'; //QQ超人打码账号
		$pass = 'hexuan1922'; //QQ超人打码密码
		$softid = '2728';//缺省为0,作者必填自己的软件id,以保证分成收入.

		$bin = file_get_contents($file_name);
		$imgdata = bin2hex($bin); //将图片二进制转为十六进制字符串上传到服务器

		require_once 'yzm/qqsuperman/vcode.php';

		//$result为 验证码识别结果|验证码ID 的字符串,如 nmvt|4D4EADF27E8645588F6C1447D87AFBB5
		if(false == ($result = recv_byte($user,$pass,$softid,$imgdata))){
			$this->_error = "get_yzm get_yzm_by_qqsuperman recv_byte is error !! error_info:{$result}";
			return false;
		}

		$rs = explode('|',$result);
		if(empty($rs[0]) || strlen($rs[0])>10) {
			$this->_error = "get_yzm get_yzm_by_qqsuperman parse is error !! error_info:{$result}";
			return false;
		}

		return $rs[0];
	}
}